<?php
global $smarty;

$thisPage = "group_members";

$subpage = getRequestParameter("subpage");
$params = unserialize(stripslashes(getRequestParameter("params")));

TemplateHelper::useLayout("default", true);


$groupmembersTypes = array();
$groupmembersTypes[] = array(type => "ambassadors",      title => "Honorary ambassadors",         content => getTabContent("ambassadors", $smarty));
$groupmembersTypes[] = array(type => "businesspartners", title => "Business partners of Hungary", content => getTabcontent("businesspartners", $smarty));
$groupmembersTypes[] = array(type => "chambers",         title => "Foreign chambers of commerce", content => getTabContent("chambers", $smarty));
$groupmembersTypes[] = array(type => "embassies",        title => "Embassies",                    content => getTabContent("embassies", $smarty));

addDataService("getchambers");
addDataService("getambassadors");
addDataService("getembassies");
addDataService("getbusinesspartners");
addDataservice("add");
addDataService("delete");

function buildLayout($smarty) {
    global $subpage;
    global $thisPage;
    global $params;

    if ($subpage == "") {
        $smarty->assign("default", array(
            content => getContent($smarty),
            contexthelp => getContextHelp($smarty),
        ));
    } else if ($subpage == "add") {
        TemplateHelper::useLayout("raw", true);
        $smarty->assign("group_id", $params['membertype']);
        $smarty->Assign("content", $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "popup_add", true)));
    }
}

function executeService($serviceName, $params) {
    switch ($serviceName) {
        case "getchambers" : { return service_getchambers($params); }
        case "getambassadors" : { return service_getambassadors($params); }
        case "getembassies" : { return service_getembassies($params); }
        case "getbusinesspartners" : { return service_getbusinesspartners($params); }
        case "add" : { return service_add($params); }
        case "delete" : { return service_delete($params); }
    }
}

function getTabContent($tabName, $smarty) {
    switch ($tabName) {
        case "ambassadors" : { return getAmbassadorsTabcontent($smarty); }
        case "businesspartners" : { return getBusinessPartnersTabContent($smarty); }
        case "chambers" : { return getChambersTabContent($smarty); }
        case "embassies" : { return getEmbassiesTabContent($smarty); }
    }
}

function getAmbassadorsTabContent($smarty) {
    global $thisPage;

    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.ambassadors", true));
}

function getBusinessPartnersTabContent($smarty) {
    global $thisPage;

    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.businesspartners", true));
}

function getChambersTabContent($smarty) {
    global $thisPage;

    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.chambers", true));
}

function getEmbassiesTabContent($smarty) {
    global $thisPage;

    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.embassies", true));
}

function service_getchambers($params) {
    Database::loadModel("groupmembers");
    Database::getModel()->setPageData(getRequestParameter("pg"), getRequestParameter("rows"));
    $today = getdate();
    $result = Database::getModel()->getMemberList("chambers");
    $reccount = Database::getAllCount();
    $limit = intval(getRequestParameter("rows"), 10);
    return array(
        page => getRequestParameter("pg"),
        total => ceil($reccount / $limit),
        records => $reccount,
        rows => JQGridHelper::convert($result, "id"),
    );
}

function service_getembassies($params) {
    Database::loadModel("groupmembers");
    Database::getModel()->setPageData(getRequestParameter("pg"), getRequestParameter("rows"));
    $today = getdate();
    $result = Database::getModel()->getMemberList("embassies");
    $reccount = Database::getAllCount();
    $limit = intval(getRequestParameter("rows"), 10);
    return array(
        page => getRequestParameter("pg"),
        total => ceil($reccount / $limit),
        records => $reccount,
        rows => JQGridHelper::convert($result, "id"),
    );
}

function service_getBusinessPartners($params) {
    Database::loadModel("groupmembers");
    Database::getModel()->setPageData(getRequestParameter("pg"), getRequestParameter("rows"));
    $today = getdate();
    $result = Database::getModel()->getMemberList("businesspartners");
    $reccount = Database::getAllCount();
    $limit = intval(getRequestParameter("rows"), 10);
    return array(
        page => getRequestParameter("pg"),
        total => ceil($reccount / $limit),
        records => $reccount,
        rows => JQGridHelper::convert($result, "id"),
    );
}

function service_getAmbassadors($params) {
    Database::loadModel("groupmembers");
    Database::getModel()->setPageData(getRequestParameter("pg"), getRequestParameter("rows"));
    $today = getdate();
    $result = Database::getModel()->getMemberList("ambassadors");
    $reccount = Database::getAllCount();
    $limit = intval(getRequestParameter("rows"), 10);
    return array(
        page => getRequestParameter("pg"),
        total => ceil($reccount / $limit),
        records => $reccount,
        rows => JQGridHelper::convert($result, "id"),
    );
}

function service_add($params) {
    Database::loadModel("groupmembers");
    Database::getModel()->saveMemberToGroup($params['groupid'], $params['name'], $params['email'], $params['contactperson']);
    return serviceSuccessful(array());
}

function service_delete($params) {
    Database::loadModel("groupmembers");
    Database::getModel()->removeMemberFromGroup($params['ids']);
    return serviceSuccessful(array());
}

function getContent($smarty) {
    global $thisPage;
    global $groupmembersTypes;
    
    $smarty->assign("groupmemberstypes", $groupmembersTypes);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "content", true));
}

function getContextHelp($smarty) {
    global $thisPage;
    
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "contexthelp", true));
}

function getPageHeader() {
}

function getPageFooter() {
}

?>
